using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CommonSLT
{
    public partial class AdvSearchControl : DevExpress.XtraEditors.XtraUserControl
    {
        #region variants
        public event EventHandler<EventArgs> OnBtnClear_Click = null;
        public event EventHandler<EventArgs> OnBtnSearch_Click = null;
        public event EventHandler<EventArgs> OnBtnRefresh_Click = null;
        private List<Control> lstControl = new List<Control>();
        public string searchWhere = "";
        #endregion variants
        //=========================================================================================

        #region control
        public AdvSearchControl()
        {
            InitializeComponent();
            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClear.Click += new EventHandler(btnClear_Click);
        }
        #endregion control
        //=========================================================================================

        #region public functions
        public void SetSearchControl(Control ctrl)
        {
            lstControl.Add(ctrl);
            string controlType = ctrl.GetType().ToString();
            switch (controlType)
            {
                case "DevExpress.XtraEditors.ComboBoxEdit":
                    ComboBoxEdit cmbEdit = (ComboBoxEdit)ctrl;
                    cmbEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    cmbEdit.KeyUp += new KeyEventHandler(control_KeyUp);
                    break;
                case "DevExpress.XtraEditors.DateEdit":
                    DateEdit dateEdit = (DateEdit)ctrl;
                    dateEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    dateEdit.KeyUp += new KeyEventHandler(control_KeyUp);
                    break;
                case "DevExpress.XtraEditors.LookUpEdit":
                    LookUpEdit lookUpEdit = (LookUpEdit)ctrl;
                    lookUpEdit.Properties.ValueMember = "Key";
                    lookUpEdit.Properties.DisplayMember = "Value";
                    lookUpEdit.Properties.CloseUpKey = new DevExpress.Utils.KeyShortcut(Keys.Escape);
                    lookUpEdit.KeyUp += new KeyEventHandler(control_KeyUp);
                    break;
                case "DevExpress.XtraEditors.TextEdit":
                    TextEdit textEdit = (TextEdit)ctrl;
                    textEdit.KeyUp += new KeyEventHandler(control_KeyUp);
                    break;
            }
        }

        public string GetWhere()
        {
            searchWhere = "";
            string controlType = "";
            string searchType = "";
            string fieldName = "";
            int i = 0;
            foreach (Control ctrl in lstControl)
            {
                i = ctrl.Name.IndexOf("_");
                searchType = ctrl.Name.Substring(0, i - 1);
                fieldName = ctrl.Name.Remove(0, i);

                controlType = ctrl.GetType().ToString();
                switch (controlType)
                {
                    case "DevExpress.XtraEditors.ComboBoxEdit":
                        ComboBoxEdit cmbEdit = (ComboBoxEdit)ctrl;
                        if (cmbEdit.Text != "" && cmbEdit.Text != null)
                            searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " = N'" + cmbEdit.Text + "'";
                        break;
                    case "DevExpress.XtraEditors.DateEdit":
                        DateEdit dateEdit = (DateEdit)ctrl;
                        if (dateEdit.EditValue != null)
                        {
                            if (searchType == "From")
                                searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " >= convert(datetime,'" + ((DateTime)dateEdit.EditValue).ToString("yyyy-MM-dd") + " 00:00:00',120)";
                            else
                                if (searchType == "End")
                                    searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " <= convert(datetime,'" + ((DateTime)dateEdit.EditValue).ToString("yyyy-MM-dd") + " 23:59:59',120)";
                                else
                                    searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " >= convert(datetime,'" + ((DateTime)dateEdit.EditValue).ToString("yyyy-MM-dd") + " 00:00:00',120) AND " + fieldName + " <= convert(datetime,'" + ((DateTime)dateEdit.EditValue).ToString("yyyy-MM-dd") + " 23:59:59',120)";
                        }
                        break;
                    case "DevExpress.XtraEditors.LookUpEdit":
                        LookUpEdit lookUpEdit = (LookUpEdit)ctrl;
                        if (lookUpEdit.EditValue != null)
                            searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " = " + ((int)lookUpEdit.EditValue).ToString();
                        break;
                    case "DevExpress.XtraEditors.TextEdit":
                        TextEdit textEdit = (TextEdit)ctrl;
                        if (textEdit.EditValue != null && textEdit.Text != "")
                            searchWhere += (searchWhere == "" ? "" : " AND ") + fieldName + " LIKE N'%" + textEdit.Text + "%'";
                        break;
                }
            }
            return searchWhere;
        }
        #endregion public functions
        //=========================================================================================

        #region events
        private void control_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnSearch_Click(null, null);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (OnBtnClear_Click != null)
                OnBtnClear_Click(sender, e);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (OnBtnSearch_Click != null)
                OnBtnSearch_Click(sender, e);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (OnBtnRefresh_Click != null)
                OnBtnRefresh_Click(sender, e);
        }
        #endregion events
        //=========================================================================================
    }
}
